/**
 * KopsoxFramework
 * Copyright (C) 2010 Free Software Foundation, Inc. <http://fsf.org/>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.kopsox.util.data.impl;

import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;

import org.kopsox.util.data.DateConverter;
import org.kopsox.util.data.FormatsEnum;
import org.kopsox.util.data.StringEnum;

/**
 * @author Konrad Renner - 04.10.2010
 *
 */
public final class DateConverterImpl implements DateConverter {
        
        @Override
        public Date convertToSQLDate(String date) {
                java.util.Date dat = convert(date, FormatsEnum.DATE_FORMATS.getFormats());
                return dat==null?null:new Date(dat.getTime());
        }

        @Override
        public Time convertToSQLTime(String date) {
                java.util.Date dat = convert(date, FormatsEnum.TIME_FORMATS.getFormats());
                return dat==null?null:new Time(dat.getTime());
        }

        @Override
        public java.util.Date convert(String date) {
                return convert(date,FormatsEnum.DATE_FORMATS.getFormats());
        }
        
        private final java.util.Date convert(final String date,final Collection<String> formats) {
                if(date != null) {
                	String trimdate = date.trim();
                    for(String dat:formats) {
                        if(dat.length() == trimdate.length() && areDelimiterAtSamePosition(dat, trimdate)) {
                            try {
                                    SimpleDateFormat formatter = new SimpleDateFormat(dat);
                                    return formatter.parse(trimdate) ;
                                        
		                    } catch (ParseException e) {
		                            //nothing to do
		                    }
                        }
                    }
                }
                
                return null;
        }
        
        private boolean areDelimiterAtSamePosition(String pattern, String toParse) {
                return pattern.indexOf(StringEnum.MINUS.toString()) == toParse.indexOf(StringEnum.MINUS.toString()) && pattern.indexOf(StringEnum.POINT.toString()) == toParse.indexOf(StringEnum.POINT.toString())
                                && pattern.indexOf(StringEnum.SLASH.toString()) == toParse.indexOf(StringEnum.SLASH.toString()) && pattern.indexOf(StringEnum.COLON.toString()) == toParse.indexOf(StringEnum.COLON.toString());
        }
}
